package com.oedtech.esl.service.eslService;

import com.alibaba.fastjson.JSONObject;
import com.oedtech.esl.json.AppJsonResult;
import com.oedtech.esl.model.SNModel.SNEslFirmware;
import com.oedtech.esl.model.esl.EslFirmware;
import com.oedtech.esl.model.esl.EslStatus;
import com.oedtech.esl.model.esl.EslTab;
import com.oedtech.esl.model.sys.StoreBsid;
import com.oedtech.esl.repository.esl.EslFirmwareDao;
import com.oedtech.esl.repository.esl.EslImgDao;
import com.oedtech.esl.repository.esl.EslStatusDao;
import com.oedtech.esl.repository.esl.EslTabDao;
import com.oedtech.esl.repository.sys.StoreBsidDao;
import com.oedtech.esl.service.SNService.InterfacesService;
import com.oedtech.esl.service.nettyService.DataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

/**
 * @Author QiuLongYu
 * @Date 2019/3/27
 */
@Service
public class EslTabService {


    @Autowired
    private StoreBsidDao storeBsidDao;
    @Autowired
    private EslFirmwareDao eslFirmwareDao;
    @Autowired
    private EslStatusDao eslStatusDao;
    @Autowired
    private EslTabDao eslTabDao;
    @Autowired
    private EslImgDao eslImgDao;

    @Value("${sn.firmwarePrefix}")
    private String vendorFlag;
    private static final Logger LOGGER = LoggerFactory.getLogger(EslTabService.class);


    public boolean checkEsl(String tinyIP,String[]menuCode){
        String eslStoreCode = storeBsidDao.findStoreCodeByTinyIP(tinyIP);
        if(eslStoreCode==null||!eslStoreCode.equals(menuCode[0])){
            LOGGER.warn("scanner bind/unbind fail: esl not exist or esl not in the store--tinyIP[{}],storeCode[{}]",tinyIP,menuCode[0]);
            return false;
        }
        return true;
    }

    /**
     * 获取所有固件id信息，并封装到map中，map的key为固件id,value为固件信息
     * @return
     */
    public Map<String,SNEslFirmware> findAllFirmware(){
        List<EslFirmware> list=eslFirmwareDao.findAll();
        Map<String, SNEslFirmware> map = new HashMap<>();
        for(EslFirmware eslFirmware:list){
            SNEslFirmware snEslFirmware=new SNEslFirmware(eslFirmware,vendorFlag);
            map.put(snEslFirmware.getId(), snEslFirmware);
        }
        return map;
    }

    public List<EslTab>findAllEsl(){
        return eslTabDao.findAll();
    }


    /**
     * 刷新数据
     * @param tinyIPs
     */
    public void refresh(String[]tinyIPs){
        List<String> list = Arrays.asList(tinyIPs);
        eslTabDao.refreshEslImg(list);
    }

    /**
     * 删除esl标签
     * @param eslIds 标签ip数组
     */
    @Transactional
    public void deleteEsl(String[]eslIds){
        List<String> eslIdList = Arrays.asList(eslIds);
        eslTabDao.deleteEsl(eslIdList);
        eslStatusDao.deleteByTinyIPs(eslIdList);
        eslImgDao.deleteByTinyIP(eslIdList);

    }

    @Autowired
    private InterfacesService interfacesService;

    public void refreshDemoImg(String[]tinyIPs,Integer inch) throws IOException {
        String inputName;
        if(inch==42){
           inputName="sn_img_file/4.2base64.txt";
        }else if(inch==29){
            inputName="sn_img_file/2.9base64.txt";
        }else {
            return;
        }
        File file=new File(inputName);
        FileInputStream fis = new FileInputStream(file);
        byte[] b = new byte[(int) file.length()];
        fis.read(b);
        fis.close();

        JSONObject jo = new JSONObject();

        jo.put("sid","");
        jo.put("back_url", "");
        jo.put("name", "屏幕测试demo图");
        jo.put("default_page_id","0");


        List<Map<String, Object>> pageList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("id","0");
        map.put("name", "测试");
        map.put("image", new String(b));
        pageList.add(map);
        jo.put("pages", pageList);
        for(String tinyIP:tinyIPs){
            interfacesService.imgManager("demo", tinyIP, jo);
        }


    }


}
